/*
 * WireDescription.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */

package engine;

import java.util.*;
import middle.*;

@SuppressWarnings("serial")
class WireDescription extends Symbol{

	public static final int vectored = 0;
	public static final int scalared = 1;

	public static int defaultExpandType = vectored;
	public static int defaultNetType = Assignable.typeWire;

	int nStart, nEnd;
	ExpressionDescription msb, lsb;
	int expandType;
	int netType;

	/**
	 * Net delays
	 */
	Delay3Description delays;

	/**
	 * Trireg strength
	 */
	byte noDriveStrength;

	WireDescription(String name, int lineNo, ExpressionDescription msb, 
			ExpressionDescription lsb, int expandType, int netType,
			byte noDriveStrength, Delay3Description delays){
		super(name, lineNo);
		this.msb = msb;
		this.lsb = lsb;
		this.expandType = expandType;
		this.delays = delays;
		this.netType = netType;
		this.noDriveStrength = noDriveStrength;
		xConsole.debug("new wire [" + nStart + ":" + nEnd + "] " + name);
	}

	WireDescription(String name, int lineNo, ExpressionDescription msb, 
			ExpressionDescription lsb, int expandType, int netType,
			Delay3Description delays){
		this(name, lineNo, msb, lsb, expandType, netType, Strength.defaultTriregStrength, delays);
	}

	public void setRange(int nStart, int nEnd){
		this.nStart = nStart;
		this.nEnd = nEnd;
	}

	public Object instantiate(NameSpace ns, ScopeNode thisScope)throws ParseException{
		try{
			nStart = (int)(msb == null ? 0 : msb.instantiate(ns).evaluate().getInt().value());
			nEnd = (int)(lsb == null ? 0 : lsb.instantiate(ns).evaluate().getInt().value());
		}catch(Exception e){
			throw new ParseException(ns.desc.toString(lineNo) + ": error:cannot evaluate LSB or MSB for wire " + this);
		}
		Wire w;
		try{
			switch(netType){
			case Assignable.typeTrireg:
				w = new Trireg(ns, this, netType, nStart, nEnd, expandType,
						noDriveStrength, delays.instantiate(ns));
				break;
			default:
				w = new Wire(ns, this, netType, nStart, nEnd, expandType,
						delays.instantiate(ns));
			}
		}catch(InterpretTimeException e){
			if(xConsole.__debug__)e.printStackTrace();
			throw new ParseException(ns.desc.toString(lineNo) + ": error:cannot evaluate delays for wire " + this);
		}
		(new WatchMonitor(w, thisScope.addLeaf(Constants.wireIcon, name, w))).trigger();
		main.historyManager.registerVariable(w, ns + "." + name);
		return w;
	}

	public int getType(){
		return netType;
	}

}




